package com.cema.manage.project.android.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.project.android.result.JsonResult;
import com.cema.manage.project.manage.proBanner.domain.ProBanner;
import com.cema.manage.project.manage.proBanner.service.IProBannerService;
import com.cema.manage.project.manage.proCategory.domain.ProCategory;
import com.cema.manage.project.manage.proCategory.service.IProCategoryService;
import com.cema.manage.project.manage.proCollect.domain.ProCollect;
import com.cema.manage.project.manage.proCollect.service.IProCollectService;
import com.cema.manage.project.manage.proGoods.domain.ProGoods;
import com.cema.manage.project.manage.proGoods.domain.ProGoodsVo;
import com.cema.manage.project.manage.proGoods.service.IProGoodsService;
import com.cema.manage.project.manage.proLike.domain.ProLike;
import com.cema.manage.project.manage.proLike.service.IProLikeService;
import com.cema.manage.project.manage.proOrder.domain.ProOrder;
import com.cema.manage.project.manage.proOrder.service.IProOrderService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@RequestMapping("pro")
@RestController
public class BizProController {
    @Resource
    private IProBannerService iProBannerService;

    @Resource
    private IProGoodsService iProGoodsService;

    @Resource
    private IProCollectService iProCollectService;

    @Resource
    private IProCategoryService iProCategoryService;

    @Resource
    private IProLikeService iProLikeService;

    @Resource
    private IProOrderService iProOrderService;

    @PostMapping("/banners")
    public JsonResult getList() {
        EntityWrapper<ProBanner> entityWrapper = new EntityWrapper<>();
        ProBanner proBanner = new ProBanner();
        proBanner.setPbEnable(1);
        entityWrapper.setEntity(proBanner);
        entityWrapper.orderBy("pb_sort", true);
        return JsonResult.returnResult(JsonResult.SUCCESS, iProBannerService.selectList(entityWrapper));
    }

    @PostMapping("/goods")
    public JsonResult getGood(ProGoodsVo proGoodsVo) {
        EntityWrapper<ProGoods> entityWrapper = new EntityWrapper<>();
        Integer pcId = proGoodsVo.getId();
        if (ObjectUtil.isNotNull(pcId)) {
            entityWrapper.eq("pc_id", pcId);
        }
        List<ProGoods> goodsList = iProGoodsService.selectList(entityWrapper);
        return JsonResult.returnResult(JsonResult.SUCCESS, goodsList);
    }

    @PostMapping("/good")
    public JsonResult getGoodById(ProGoods proGoods) {
        ProGoods goods = iProGoodsService.selectById(proGoods.getId());
        ProGoodsVo proGoodsVo = BeanUtil.toBean(goods, ProGoodsVo.class);
        EntityWrapper entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("pg_id", proGoods.getId());
        List<ProCollect> proCollectList = iProCollectService.selectList(entityWrapper);
        if (ObjectUtil.isNotNull(proCollectList) && proCollectList.size() > 0) {
            String userIds = proCollectList.stream().map(ProCollect::getUsId).map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA));
            proGoodsVo.setUserIds(userIds);
            proGoodsVo.setPgCount(proCollectList.size());
        } else {
            proGoodsVo.setUserIds(StrUtil.EMPTY);
            proGoodsVo.setPgCount(0);
        }
        List<ProLike> proLikeList = iProLikeService.selectList(entityWrapper);
        if (ObjectUtil.isNotNull(proLikeList) && proLikeList.size() > 0) {
            String userIds = proLikeList.stream().map(ProLike::getUsId).map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA));
            proGoodsVo.setUserIdLikes(userIds);
            proGoodsVo.setPgCountLike(proLikeList.size());
        } else {
            proGoodsVo.setUserIdLikes(StrUtil.EMPTY);
            proGoodsVo.setPgCountLike(0);
        }
        List<ProOrder> orderList = iProOrderService.selectList(entityWrapper);
        if(ObjectUtil.isNotNull(orderList) && orderList.size() > 0){
            String userIds = orderList.stream().map(ProOrder::getUsId).map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA));
            proGoodsVo.setUserIdOrders(userIds);
        }else{
            proGoodsVo.setUserIdOrders(StrUtil.EMPTY);
        }
        return JsonResult.returnResult(JsonResult.SUCCESS, proGoodsVo);
    }

    @PostMapping("/category")
    public JsonResult getCategory() {
        EntityWrapper<ProCategory> entityWrapper = new EntityWrapper<>();
        entityWrapper.orderBy("pc_sort", true);
        return JsonResult.returnResult(JsonResult.SUCCESS, iProCategoryService.selectList(entityWrapper));
    }
}
